package model;

import Repository.Repository;
import Repository.RepositoryFactory;
import constants.SMSConstants;

public class Prepared implements SMSObjectState {

	private final String NAME = "Prepared";
	RuleSelector ruleSelector;
	NextStateCalculator stateCalculator;
	CalculateNextStateStrategy strategy;
	Repository repository;

	public void execute(SMSObject smsObj) {
		if (smsObj instanceof Lead) {
			// System.out.println("Executing Prepared State...");
			ruleSelector = new ApproveRuleSelector(smsObj);

			repository = RepositoryFactory.getRepository(SMSConstants.LEAD);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,
					ruleSelector);
			// System.out.println("New state..." + newState.getStateName());
			if (newState.getStateName().equals("Merged")) {
				System.out
						.println("****Lead can not be qualified as the object doesnot pass the rules****");
				smsObj.setCurrentState(newState);
				repository.update(smsObj.getSmsObjId(), smsObj);
				return;
			} else if (newState.getStateName().equals("Qualified")) {
				// System.out.println("qualified as the object passed the lead qualifying rules");
				smsObj.setCurrentState(newState);
				repository.update(smsObj.getSmsObjId(), smsObj);
				System.out.println("Successfully qualified lead");

			}

		} else if (smsObj instanceof Opportunity) {
			// System.out.println("Executing Prepared State...");
			ruleSelector = new QuoteRuleSelector(smsObj);

			repository = RepositoryFactory
					.getRepository(SMSConstants.OPPORTUNITY);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,
					ruleSelector);
			// System.out.println("New state..." + newState.getStateName());
			if (newState.getStateName().equals(NAME)) {
				System.out
						.println("Cannot be quoted as the object doesnot passed the producing Quote rules");
				return;
			}
			smsObj.setCurrentState(newState);
			repository.update(smsObj.getSmsObjId(), smsObj);
			System.out.println("Successfully quoted opportunity");

		} else if (smsObj instanceof Contract) {
			Contract c = (Contract) smsObj;
			// System.out.println("Executing Prepared State...");
			ruleSelector = new ApproveRuleSelector(smsObj);
			Repository contractRepository = RepositoryFactory
					.getRepository(SMSConstants.CONTRACT);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(smsObj,
					ruleSelector);
			 //System.out.println("New state..." + newState.getStateName());
			if (newState.getStateName().equals("Imported")) {
				System.out
						.println("Cannot be Approved as the object doesnot pass the Contract rules");
				smsObj.setCurrentState(newState);
				((Contract)smsObj).setNextApprover(-1);
				((Contract)smsObj).setCurrentLevel(0);
				contractRepository.update(smsObj.getSmsObjId(), smsObj);
				return;
			} else {
				// System.out.println("Approved as the object passed the Approving Contract rules");
				c.setCurrentState(newState);
				c.setCurrentLevel(c.getCurrentLevel() + 1);
				contractRepository.update(c.getSmsObjId(), c);
				if (newState.getStateName().equals("Approved")) {
					System.out.println("Contract successfully passed all levels of approval.");
				}
			}
		}
	}

	public String getStateName() {
		return NAME;
	}

}
